/*
 * @copyright IMTT. Co. Ltd.
 * 
 * Here is detailed comment.
 */
package com.apis.web.goods;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apis.bussiness.entity.impl.GoodsImpl;
import com.apis.bussiness.entries.impl.GoodsMgrImpl;
import com.apis.web.Action;
import com.apis.web.WebConstants;
import com.apis.web.XmlResponse;


/**
 * @author LiHongChun
 * @date 2012-10-25
 * @copyright IMTT. Co. Ltd.
 */
public class GetAllGoods extends Action {

	/* (non-Javadoc)
	 * @see com.imtt.insect.web.Action#execute(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub

		XmlResponse xml = new XmlResponse();
		xml.setRequest(Request(req));
		int pageIndex = 1;
		int pageSize = 20;
		String pIndex = req.getParameter(WebConstants.TAG_PAGE_INDEX);
		if (pIndex != null) {
			pageIndex = Integer.parseInt(pIndex);
		}
		String pSize = req.getParameter(WebConstants.TAG_PAGE_SIZE);
		if (pSize != null) {
			pageSize = Integer.parseInt(pSize);
		}
		StringBuilder sb = new StringBuilder();
		try {
			GoodsMgrImpl manager = new GoodsMgrImpl();
			List<Object> GoodsList = manager.getList(GoodsImpl.class);
			
			long rt = GoodsList.size();
			long pt = rt / pageSize;
			if (rt % pageSize > 0) {
				pt += 1;
			}

			sb.append("<"+ WebConstants.TAG_RECORD_NUMBER +">").append(rt).append("</"+ WebConstants.TAG_RECORD_NUMBER +">");
			sb.append("<"+ WebConstants.TAG_PAGE_NUMBER +">").append(pt).append("</"+ WebConstants.TAG_PAGE_NUMBER +">");
			sb.append("<"+ WebConstants.TAG_PAGE_INDEX +">").append(pageIndex).append("</"+ WebConstants.TAG_PAGE_INDEX +">");
			sb.append("<"+ WebConstants.TAG_PAGE_SIZE +">").append(pageSize).append("</"+ WebConstants.TAG_PAGE_SIZE +">");
			sb.append("<"+ WebConstants.TAG_COLUMN_NUMBER +">").append(15).append("</"+ WebConstants.TAG_COLUMN_NUMBER +">");
			sb.append("<"+ WebConstants.TAG_METADATA_COLUMN +" "+ WebConstants.TAG_METADATA_NAME +"=\""+ WebConstants.TAG_GOODS_ID +"\" "+ WebConstants.TAG_METADATA_DISPLAYNAME +"=\"编号\" />");
			sb.append("<"+ WebConstants.TAG_METADATA_COLUMN +" "+ WebConstants.TAG_METADATA_NAME +"=\""+ WebConstants.TAG_GOODS_NAME +"\" "+ WebConstants.TAG_METADATA_DISPLAYNAME +"=\"商品名称\" />");
			sb.append("<"+ WebConstants.TAG_METADATA_COLUMN +" "+ WebConstants.TAG_METADATA_NAME +"=\""+ WebConstants.TAG_GOODS_CODE +"\" "+ WebConstants.TAG_METADATA_DISPLAYNAME +"=\"编码\" />");
			sb.append("<"+ WebConstants.TAG_METADATA_COLUMN +" "+ WebConstants.TAG_METADATA_NAME +"=\""+ WebConstants.TAG_GOODS_PRICE +"\" "+ WebConstants.TAG_METADATA_DISPLAYNAME +"=\"价格\" />");
			sb.append("<"+ WebConstants.TAG_SORT_BY +">").append(WebConstants.TAG_GOODS_ID).append("</"+ WebConstants.TAG_SORT_BY +">");
			sb.append("<"+ WebConstants.TAG_SORT_SEQUENCE +">").append("ASC").append("</"+ WebConstants.TAG_SORT_SEQUENCE +">");
			xml.setMetaData(sb.toString());
			sb.delete(0, sb.length());
			
			for (Object goods : GoodsList) {
				GoodsImpl temp = (GoodsImpl)goods;
				sb.append("<goods>");
				sb.append("<"+ WebConstants.TAG_GOODS_ID +">").append(temp.getId()).append("</"+ WebConstants.TAG_GOODS_ID +">");
				sb.append("<"+ WebConstants.TAG_GOODS_NAME +">").append(temp.getName()).append("</"+ WebConstants.TAG_GOODS_NAME +">");
				sb.append("<"+ WebConstants.TAG_GOODS_CODE +">").append(temp.getCode()).append("</"+ WebConstants.TAG_GOODS_CODE +">");
				sb.append("<"+ WebConstants.TAG_GOODS_PRICE +">").append(temp.getPrice()).append("</"+ WebConstants.TAG_GOODS_PRICE +">");
				sb.append("</goods>");
			}
			xml.setData(sb.toString());
			xml.setState(1);
			sb = null;
		} catch (Exception e) {
			e.printStackTrace();
			String reason = "操作失败: 未知错误...";
			xml.setReason(reason);
			xml.setException(e);
		}
		Response(resp, xml.toString());
	}

	/* (non-Javadoc)
	 * @see com.imtt.insect.web.Action#getActionID()
	 */
	@Override
	public int getActionID() {
		// TODO Auto-generated method stub
		return 1005;
	}

}
